package com.example.tester.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tester.annotation.LogWeb;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @author lzh
 * @date 2019/9/5 - 10:32
 */
@RestController
@Slf4j
public class HelloCotroller {


//    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @PostMapping("/uploadMore")
    @ResponseBody
    public String handleFileUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            String filePath = "D://aim//";
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(
                            new File(filePath + file.getOriginalFilename())));//设置文件路径及名字
                    stream.write(bytes);// 写入
                    stream.close();
                } catch (Exception e) {
                    stream = null;
                    return "第 " + i + " 个文件上传失败 ==> "
                            + e.getMessage();
                }
            } else {
                return "第 " + i
                        + " 个文件上传失败因为文件为空";
            }
        }
        return "上传成功";
    }






    @GetMapping("/test")
    @LogWeb("test")
    public String test(){
        //日志级别
        // 从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL，
        // 如果设置为WARN，则低于WARN的信息都不会输出
        log.trace("日志输出 trace");
        log.debug("日志输出 debug");
        log.info("日志输出 info");
        log.warn("日志输出 warn");
        log.error("日志输出 error");

        return "这是一个测试132wf";
    }













    /**
     *
     * <form action="${pageContext.request.contextPath }/checkBox"
     *         method="post">
     *         <input type="checkbox" name="hobby" value="a1" />a1<br />
     *         <input type="checkbox" name="hobby" value="a2" />a2<br />
     *         <input type="checkbox" name="hobby" value="a3" />a3<br />
     *         <input type="checkbox" name="hobby" value="a4" />a4<br />
     *         <input type="checkbox" name="hobby" value="a5" />a5<br />
     *         <input type="submit"/>
     *     </form>
     *
     * @param hobby
     * @return
     */
    @PostMapping("/checkBox")
    public String checkBox(@RequestParam(value = "hobby", required = false) String[] hobby){

        if (hobby == null){
            return "";
        }else  if (hobby.length > 0){
            String result = Arrays.toString(hobby);
            return result;
        }else {
            return "";
        }
    }





    @GetMapping("/hello")
    @LogWeb("hello")
    public String hello(@RequestParam String name){
        return name+"这是一个测试!!!1";
    }


    @GetMapping("/hello2")
    @LogWeb("hello2")
    @ResponseBody
    public Object hello2(@RequestParam String name ,
                         @RequestParam  String  age){

        JSONObject jo = new JSONObject();
        jo.put("name",name);
        jo.put("age",age);

        return jo;
    }



}
